package com.yuntsg.nnsfcp.controller.back;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yuntsg.nnsfcp.common.beans.ResultBean;
import com.yuntsg.nnsfcp.common.utils.CheckUtil;
import com.yuntsg.nnsfcp.front.entity.Employee;
import com.yuntsg.nnsfcp.front.entity.TRole;
import com.yuntsg.nnsfcp.front.mapper.EmployeeMapper;
import com.yuntsg.nnsfcp.front.mapper.TPermissionMapper;
import com.yuntsg.nnsfcp.front.mapper.TRoleMapper;
import com.yuntsg.nnsfcp.front.mapper.TUserRoleMapper;
import com.yuntsg.nnsfcp.service.CommonService;
import com.yuntsg.nnsfcp.service.back.Role.EmRoleService;
import com.yuntsg.nnsfcp.util.AuthCode;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@RequestMapping("/sys/user")
@RestController
@Slf4j
//@Api(tags = "组织模块-人员角色权限管理")
public class EmployeeController {

    //2021年1月22日10:41:43 修改人员角色

    @Resource
    EmployeeMapper employeeMapper;
    @Resource
    TUserRoleMapper tUserRoleMapper;
    @Resource
    TRoleMapper tRoleMapper;
    @Resource
    EmRoleService emRoleService;
    @Resource
    CommonService commonService;
    @Resource
    private TPermissionMapper tPermissionMapper;

    //@ApiOperation(value = "获取全部角色")
    @GetMapping("/get-all-roles")
    public ResultBean<Object> getAllRoles() {
        List<TRole> tRoles = tRoleMapper.selectList(new LambdaQueryWrapper<>());
        return new ResultBean<>(tRoles);
    }


    @GetMapping("/get-all-user")
//    @ApiImplicitParams({
//            @ApiImplicitParam(paramType = "query", name = "username", value = "用户名", required = false, dataType = "String"),
//            @ApiImplicitParam(paramType = "query", name = "role", value = "权限", required = false, dataType = "String"),
//            @ApiImplicitParam(paramType = "query", name = "type", value = "查询类型 1 是默认管理员 2 是查询自己信息", required = false, dataType = "String"),
//            @ApiImplicitParam(paramType = "query", name = "status", value = "状态 1是正常 0是删除", required = false, dataType = "String")
//    })
    //@ApiOperation(value = "获取后台所有用户")
    public ResultBean<Object> findAll(String username, Integer role, Integer status, Integer type) {
        /*
         *  根据判断 只有管理员可以获取全部用户
         *  非管理员只能获取自己的用户
         *  2021年1月26日08:53:08  这块全部按照统一进行处理 只有单位管理员可以看到
         * */
//        QueryWrapper queryWrapper = new QueryWrapper();
//        if (session.getAttribute("role").toString().equals("4")) {
        if (type == null) {
            log.error("type==null 默认给1 " + type);
            type = 1;
        }
        return new ResultBean<>(emRoleService.handleRols(username, role, status, type));
//        }
//        else {
//            int uid = userUtil.getBackUserInfo().getId();
//            queryWrapper.eq("id", uid);
//            List<Employee> userLS = employeeMapper.selectList(queryWrapper);
//            userLS.forEach(user -> {
//                user.setFlag(1);
//            });
//            return new ResultBean<>(userLS);
//        }
    }


    /*
     * 2019年9月23日15:03:37
     * 根据广州的需求进行更改，
     * 好麻烦
     * */

    @PostMapping("/add-user")
    //@ApiOperation(value = "添加用户", notes = "传递用户一些参数 参数 userName 手机号 mobile 密码  password 角色 roleids 用,隔开")
    public ResultBean<Object> addUser(@RequestBody Employee employee) {
        employee.setCreateTime(LocalDateTime.now());
        employee.setStatus(1);
        String pre = employee.getPassword();
        employee.setPassword(AuthCode.encodePass(employee.getPassword()));
        String phone = employee.getMobile();
        if (employeeMapper.selectCount(new QueryWrapper<Employee>().eq("mobile", phone)) == 1) {
            CheckUtil.check(false, "重复手机号");
        }
        employee.setOpenPassword(pre);
        boolean b = emRoleService.insertEm(employee);
        CheckUtil.check(b, "插入失败");
        return new ResultBean<>("");
    }

//    @GetMapping("/test-exception")
//    //@ApiOperation(value = "测试异常回滚操作的数据库")
//    public ResultBean<Object> testException() {
//        emRoleService.testException();
//        return new ResultBean<>();
//    }


    @PostMapping("/update-user-role")
    //@ApiOperation(value = "修改用戶角色", notes = "传递用户一些参数 只需要传递人员id 和角色roleids 参数就可以")
    public ResultBean<Object> updateUserRole(@RequestBody Employee employee) {
        if (StringUtils.isEmpty(employee.getId()) || StringUtils.isEmpty(employee.getRoleids())) {
            CheckUtil.check(false, "请传递人员和id 或者角色ids");
        }
        if (StringUtils.isEmpty(employee.getMobile())) {
            CheckUtil.check(false, "请传递人员手机号");
        }
        if (StringUtils.isEmpty(employee.getUserName())) {
            CheckUtil.check(false, "请传递人员姓名");
        }
        employee.setOpenPassword(employee.getPassword());
        employee.setPassword(AuthCode.encodePass(employee.getPassword()));
        String phone = employee.getMobile();
        Employee mobiles = employeeMapper.selectOne(new QueryWrapper<Employee>().eq("mobile", phone));
        if (mobiles != null && mobiles.getId() != employee.getId()) {
            CheckUtil.check(false, "重复手机号已经被注册");
        }

        employeeMapper.updateById(employee);
        boolean b = emRoleService.updateEmRole(employee.getId(), employee.getRoleids());
        CheckUtil.check(b, "更新失败");
        return new ResultBean<>("");
    }


    @PostMapping("/update-password")
    //@ApiOperation(value = "修改密码")
    public ResultBean<Object> updatePass(@RequestBody Employee employee) {
//        Object attribute = session.getAttribute("role");
//        Object uid = session.getAttribute("uid");
//        if (!attribute.toString().equals("4")) {
//            if (Integer.parseInt(uid.toString()) != employee.getId()) {
//                CheckUtil.check(false, "只能修改自己的密码");
//            }
//        }
        employee.setPassword(AuthCode.encodePass(employee.getOpenPassword()));
        int i = employeeMapper.updateById(employee);
        return new ResultBean<>(i);
    }

    @GetMapping("/update-user-status")
//    //@ApiOperation(value = "删除用户，启用用户 1 启用 0是删除")
//    @ApiImplicitParams({
//            @ApiImplicitParam(paramType = "query", name = "id", value = "用户id", required = true, dataType = "String"),
//            @ApiImplicitParam(paramType = "query", name = "status", value = "状态 1是正常 0是删除", required = true, dataType = "String")
//    })
    public ResultBean<Object> updateUserStatus(Integer id, String status) {
//        Object attribute = session.getAttribute("role");
//        if (!attribute.toString().equals("4")) {
//            CheckUtil.check(false, "只能管理员操作");
//        }
        Employee employee = employeeMapper.selectById(id);
        employee.setStatus(Integer.parseInt(status));
        int i = employeeMapper.updateById(employee);
        return new ResultBean<>(i);
    }


}
